//
//  ShakeViewController.m
//  FlyGift
//
//  Created by lanou3g on 15/7/17.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ShakeViewController.h"
#import "LZAudioTool.h"
#import "UIView+LZ.h"

@interface ShakeViewController ()

@property(nonatomic,strong)UIImageView * back;
@property(nonatomic,strong)UIImageView * up;
@property(nonatomic,strong)UIImageView * down;

@end

@implementation ShakeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView *back = [[UIImageView alloc] init];
    back.image = [UIImage imageNamed:@"bg_shaking"];
    back.frame = self.view.bounds;
    [self.view addSubview:back];
    self.back = back;
    
    
    UIImageView *up = [[UIImageView alloc] init];
    up.image = [UIImage imageNamed:@"bg_yaoyao_above"];
    up.frame = CGRectMake(-kP6width(50), 0, self.view.width * 1.2, self.view.height * 0.5);
    [back addSubview:up];
    self.up = up;
    
    
    UIImageView *down = [[UIImageView alloc] init];
    down.image = [UIImage imageNamed:@"bg_yaoyao_under"];
    down.frame = CGRectMake(-kP6width(50), self.view.height * 0.5, self.view.width * 1.2, self.view.height * 0.5);
    [back addSubview:down];
    self.down = down;


}
-(BOOL)canBecomeFirstResponder
{
    return YES;
}


-(void)viewDidAppear:(BOOL)animated {
    
    [super viewDidAppear:animated];
    
    [self becomeFirstResponder];
    
}


-(void)viewWillDisappear:(BOOL)animated {
    
    [self resignFirstResponder];
    
    [super viewWillDisappear:animated];
    
}

#pragma mark - 实现相应的响应者方法
/** 开始摇一摇 */
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"motionBegan");
    CGFloat offset = self.back.height/2;
    CGFloat duration = 0.4;
    [UIView animateWithDuration:duration animations:^{
        self.up.y -= offset;
        self.down.y += offset;
    }];
    [LZAudioTool playMusic:@"dance.mp3"];
    [MBProgressHUD showSuccess:@"再抖抖"];
}

/** 摇一摇结束（需要在这里处理结束后的代码） */
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    // 不是摇一摇运动事件
    if (motion != UIEventSubtypeMotionShake) return;
    NSLog(@"motionEnded");
    CGFloat offset = self.back.height / 2;
    CGFloat duration = 0.4;
    [UIView animateWithDuration:duration animations:^{
        self.up.y += offset;
        self.down.y -= offset;
    }];
}

/** 摇一摇取消（被中断，比如突然来电） */
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    NSLog(@"motionCancelled");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
